// Monitor.cs
//

namespace ServiceAdmin
{
	using ServiceAdmin.Config;
	using ServiceAdmin.Status;
    using System;
	using System.Drawing;
	using System.ComponentModel;
	using System.ServiceProcess;
    using System.Timers;
    using System.Windows.Forms;

    /// <summary>
    /// Summary description for Monitor.
    /// </summary>
    public class Monitor
    {
        private ServiceControl		 _control			= null;
        private GuiHandler			 _handler			= null;
		private	StatusCollection	 _statusCollection	= null;
        private bool				 _errorOccured			= false;


        /// <summary>
        /// Creates a new instance of Handler
        /// throws exception number 200002 - Error while initializing the service.
        /// throws exception number 200003 - Error while loading the options.
        /// </summary>
        public Monitor(GuiHandler handler)
        {
            _handler					= handler;
            _handler.OnMinimize			+= new EventHandler(Minimize);
            _handler.OnMaximize			+= new EventHandler(Maximize);
            _handler.OnQuit				+= new EventHandler(Quit);
			_handler.OnStart			+= new EventHandler(Start);
			_handler.OnStop				+= new EventHandler(Stop);
			_handler.OnRestart			+= new EventHandler(Restart);
			_handler.OnLanguageChange	+= new EventHandler(UpdateLanguage);

			if (!OptionHandler.Instance.ResourceReader.IsValidReader)
			{
				Error(null, 200003);
			}

			InitControl();
			UpdateText();
        }


		private void InitControl()
		{
			try
			{
				_control = new ServiceControl(
					OptionHandler.Instance.ResourceReader.Timer,
					OptionHandler.Instance.ResourceReader.ServiceName,
					OptionHandler.Instance.ResourceReader.MachineName);
			}
			catch (Win32Exception)
			{
				Error(null, 200002);
				return;
			}

			_control.OnStatusChange += new ServiceStatusChangeEvent(ChangeStatus);
			_control.OnError += new ServiceErrorEvent(Error);

			_statusCollection = new StatusCollection(_handler, _control);
			_control.StartTimer();
		}


		private void Start(object sender, EventArgs e)
		{
			_control.Start();
		}


		private void Restart(object sender, EventArgs e)
		{
			_control.Restart();
		}


		private void Stop(object sender, EventArgs e)
		{
			_control.Stop();
		}


		private void UpdateLanguage(object sender, EventArgs e)
		{
			UpdateText();
		}


        private void UpdateText()
        {
            // buttons
            _handler.StartButton.Text   = OptionHandler.Instance.GetLanguageRes("Start");
            _handler.ResetButton.Text   = OptionHandler.Instance.GetLanguageRes("Restart");
            _handler.StopButton.Text    = OptionHandler.Instance.GetLanguageRes("Stop");

            // tab pages
            _handler.CheckPage.Text		= OptionHandler.Instance.GetLanguageRes("Admin");
            _handler.SettingsPage.Text  = OptionHandler.Instance.GetLanguageRes("Settings");

            // title bar
            _handler.Text               = OptionHandler.Instance.GetLanguageRes("Title");

            // tray icon
            _handler.OpenItem.Text      = OptionHandler.Instance.GetLanguageRes("Open");
            _handler.QuitItem.Text      = OptionHandler.Instance.GetLanguageRes("Quit");
            _handler.StartItem.Text     = OptionHandler.Instance.GetLanguageRes("Start");
            _handler.StopItem.Text      = OptionHandler.Instance.GetLanguageRes("Stop");
            _handler.ResetItem.Text     = OptionHandler.Instance.GetLanguageRes("Restart");

			// status bar
			UpdateStatusText();
        }


		private void Minimize(object sender, EventArgs e)
		{
			_handler.WindowState = FormWindowState.Minimized;
			_handler.Hide();
		}


		private void Maximize(object sender, EventArgs e)
		{
			_handler.Show();
			_handler.Activate();
			_handler.WindowState = FormWindowState.Normal;
		}


		private void Quit(object sender, EventArgs e)
		{
			Application.Exit();
		}


		private void Error(ServiceControl sender, int errorNumber)
		{
			_errorOccured = true;
			_handler.StatusBar.Text = OptionHandler.Instance.GetLanguageRes(errorNumber.ToString());
		}


        private void ChangeStatus(ServiceControl sender, ServiceControllerStatus status)
        {
            if (!_errorOccured)
            {
				_statusCollection.SetStatus(status);
				_handler.PictureBox.Image = _statusCollection.CurrentStatus.StatusIcon.ToBitmap();
				UpdateStatusText();
            }
        }


		private void UpdateStatusText()
		{
			_handler.StatusBar.Text	= _statusCollection.CurrentStatus.StatusDescription;
			_handler.TaskIcon.Text	= _handler.Text + " - " + _statusCollection.CurrentStatus.StatusDescription;
			_handler.TaskIcon.Icon	= _statusCollection.CurrentStatus.StatusIcon;
		}
    }
}